package com.glorysoft.sunnypda.ui.reportfault

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.glorysoft.sunnypda.BR
import com.glorysoft.sunnypda.R
import com.glorysoft.sunnypda.base.BaseActivity
import com.glorysoft.sunnypda.databinding.ActivityReportFaultListBinding
import kotlinx.android.synthetic.main.activity_report_fault_list.*

/**
 * FileName: ReportRepairActivity
 * Author: XJH
 * Date: 2022/1/7 16:05
 * Description:
 * History:
 */
class ReportFaultListActivity :
    BaseActivity<ReportFaultListViewModel, ActivityReportFaultListBinding>(),
    View.OnClickListener {

    var status: Int = 0

    override fun layoutId(): Int {
        return R.layout.activity_report_fault_list
    }

    override fun initVariableId(): Int {
        return BR.reportRepairViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.fault_repair))
        llReminder.visibility = View.VISIBLE
        tvUnFinish.setOnClickListener(this)
        tvFinish.setOnClickListener(this)
        tvRunning.setOnClickListener(this)
        tvFaultRepair.setOnClickListener(this)
        viewModel.initRepairForUser(status, "")

        initEdit(etEqpWork, object : EditTextListener {
            override fun onTextChangeListener(s: String) {
                viewModel.initRepairForEqp(status, s)
            }

        })
    }

    @SuppressLint("SetTextI18n")
    override fun initData() {
        viewModel.defUI.callObserve.observe(this) {
            tvUnFinish.text =
                getString(R.string.unfinished) + "(" + viewModel.unFinishList.size + ")"
            tvFinish.text = getString(R.string.finish) + "(" + viewModel.finishList.size + ")"
            tvRunning.text = getString(R.string.in_process) + "(" + viewModel.runningList.size + ")"
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tvUnFinish -> {
                status = 0
                updateUI()
            }
            R.id.tvFinish -> {
                status = 2
                updateUI()
            }
            R.id.tvRunning -> {
                status = 1
                updateUI()
            }
            R.id.tvFaultRepair -> {
                startActivityForResult(Intent(this, ReportFaultActivity::class.java), 0)
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == RESULT_OK) {
            viewModel.initRepairForUser(status, etEqpWork.text.toString())
        }
    }


    private fun updateUI() {
        when (status) {
            0 -> {
                tvUnFinish.setBackgroundColor(
                    resources.getColor(R.color.free_ui_blue_sky_theme_color)
                )
                tvUnFinish.setTextColor(resources.getColor(R.color.white))
                tvFinish.setBackgroundColor(resources.getColor(R.color.white))
                tvRunning.setBackgroundColor(resources.getColor(R.color.white))
                tvFinish.setTextColor(resources.getColor(R.color.black))
                tvRunning.setTextColor(resources.getColor(R.color.black))
            }
            2 -> {
                tvFinish.setBackgroundColor(
                    resources.getColor(R.color.free_ui_blue_sky_theme_color)
                )
                tvFinish.setTextColor(resources.getColor(R.color.white))
                tvUnFinish.setBackgroundColor(resources.getColor(R.color.white))
                tvRunning.setBackgroundColor(resources.getColor(R.color.white))
                tvUnFinish.setTextColor(resources.getColor(R.color.black))
                tvRunning.setTextColor(resources.getColor(R.color.black))
            }
            1 -> {
                tvRunning.setBackgroundColor(
                    resources.getColor(R.color.free_ui_blue_sky_theme_color)
                )
                tvRunning.setTextColor(resources.getColor(R.color.white))
                tvUnFinish.setBackgroundColor(resources.getColor(R.color.white))
                tvFinish.setBackgroundColor(resources.getColor(R.color.white))
                tvUnFinish.setTextColor(resources.getColor(R.color.black))
                tvFinish.setTextColor(resources.getColor(R.color.black))
            }
        }

        viewModel.updateList(status, etEqpWork.text.toString())

    }

}